
package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.core.torneo.Torneo;
import model.core.utente.Commissario;
import model.core.utente.Giocatore;
import model.core.utente.Studente;
import model.persistence.mysql.DAOManager_MySQL;

/**
 * 
 * @author Giuseppe Fusco (fusgius@gmail.com)
 *
 */
public class SelezioneCommissariTorneo extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 792891353840228876L;
	private DAOManager_MySQL dao;
	private List<Integer> index;
	private List<Studente> studenti;
	private Torneo torneo;

	public SelezioneCommissariTorneo() {
		initComponents();
		try {
			dao = DAOManager_MySQL.getInstance();
			studenti = dao.getStudenteDao().load("");
			addListener();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setTorneo(Torneo torneo) {
		this.torneo = torneo;
		fillComponents("");
	}

	private void addListener() {
		// TODO Auto-generated method stub
		aggiungiPartecipante.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(listPartecipanti.getSelectedIndex()>=0){
					int id = listPartecipanti.getSelectedIndex();
					if(torneo.addCommissario(Commissario.createFromStudente(studenti.get(id))))
					{
						index.add(new Integer(id));
						listPartecipanti.setCellRenderer(new StudenteCellRenderer(index));
						nroCommissari.setText(String.valueOf(torneo.getCommissione().getAll().size()));
					}
				}
			}
		});

		eliminaPartecipante.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(listPartecipanti.getSelectedIndex()>=0){
					int id = listPartecipanti.getSelectedIndex();
					if(torneo.removeCommissario(Commissario.createFromStudente(studenti.get(id))))
					{
						index.remove(new Integer(id));
						listPartecipanti.setCellRenderer(new StudenteCellRenderer(index));
						nroCommissari.setText(String.valueOf(torneo.getCommissione().getAll().size()));
					}
				}
			}
		});

	}

	private void fillComponents(String text){
		index = new ArrayList<Integer>();
	/*	try {
			studenti = new ArrayList<Studente>();
			if(torneo!=null){
				for(Studente s: dao.getStudenteDao().load(text)){
					if(!torneo.isGiocatoreDelTorneo(Giocatore.createFromStudente(s)))
						studenti.add(s);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		listPartecipanti.setCellRenderer(new StudenteCellRenderer(index));
		listPartecipanti.setModel(new StudenteListModel(studenti));
		totPartecipanti.setText(String.valueOf(listPartecipanti.getModel().getSize()));
		nroCommissari.setText(torneo!=null?String.valueOf(torneo.getCommissione().getAll().size()):"");
	}

	private void initComponents() {

		jScrollPane2 = new javax.swing.JScrollPane();
		listPartecipanti = new javax.swing.JList();
		javax.swing.JLabel jLabel8 = new javax.swing.JLabel();
		javax.swing.JLabel jLabel11 = new javax.swing.JLabel();
		totPartecipanti = new javax.swing.JTextField();
		nroCommissari = new javax.swing.JTextField();
		javax.swing.JLabel jLabel9 = new javax.swing.JLabel();
		eliminaPartecipante = new javax.swing.JButton();
		aggiungiPartecipante = new javax.swing.JButton();

		listPartecipanti.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		listPartecipanti.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		jScrollPane2.setViewportView(listPartecipanti);

		jLabel8.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel8.setText("Commissari");

		jLabel11.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel11.setText("Totale");

		totPartecipanti.setEditable(false);
		totPartecipanti.setColumns(4);
		totPartecipanti.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N

		nroCommissari.setEditable(false);
		nroCommissari.setColumns(4);
		nroCommissari.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N

		jLabel9.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel9.setText("Totale commissari");

		eliminaPartecipante.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		eliminaPartecipante.setText("Elimina");

		aggiungiPartecipante.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		aggiungiPartecipante.setText("Aggiungi");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup()
										.addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel11)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(totPartecipanti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(26, 26, 26))
										.addGroup(layout.createSequentialGroup()
												.addComponent(jScrollPane2)
												.addGap(4, 4, 4)))
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(layout.createSequentialGroup()
																.addGap(50, 50, 50)
																.addComponent(nroCommissari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
																.addComponent(jLabel9)
																.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
																		.addComponent(eliminaPartecipante, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																		.addComponent(aggiungiPartecipante, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
																		.addContainerGap())
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel8)
								.addComponent(jLabel11)
								.addComponent(totPartecipanti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(layout.createSequentialGroup()
												.addComponent(aggiungiPartecipante)
												.addGap(0, 0, 0)
												.addComponent(eliminaPartecipante)
												.addGap(18, 18, 18)
												.addComponent(jLabel9)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(nroCommissari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGap(0, 0, Short.MAX_VALUE))
												.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE))
												.addContainerGap())
				);
	}

	private javax.swing.JButton aggiungiPartecipante;
	private javax.swing.JButton eliminaPartecipante;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JList listPartecipanti;
	private javax.swing.JTextField nroCommissari;
	private javax.swing.JTextField totPartecipanti;
}
